package com.tydic.springboot.service.impl;

import com.tydic.springboot.mapper.StudentMapper;
import com.tydic.springboot.model.Student;
import com.tydic.springboot.model.Staff;
import com.tydic.springboot.service.StudentSertvice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * @Author 714班
 */
@Service
public class StudentSertviceImpl implements StudentSertvice {

    @Autowired
    private StudentMapper studentMapper;

    @Override
    public /*synchronized*/ List<Student> getAllStudent() {
        return studentMapper.selectAllStudent();
    }

    /*@Override
    public *//*synchronized*//* List<Student> getAllStudent() {
        //字符串的序列化器
        RedisSerializer redisSerializer = new StringRedisSerializer();
        redisTemplate.setKeySerializer(redisSerializer);

        //高并发条件下，此处有点问题：缓存穿透
        //查询缓存
        List<Student> studentList = (List<Student>)redisTemplate.opsForValue().get("allStudents");

        //双重检测锁
        if (null == studentList) {
            synchronized (this) {
                //从redis获取一下
                studentList = (List<Student>)redisTemplate.opsForValue().get("allStudents");
                if (null == studentList) {
                    System.out.println("查询的数据库........");
                    //缓存为空，查询一遍数据库
                    studentList = studentMapper.selectAllStudent();
                    //把数据库查询出来的数据，放入redis中
                    redisTemplate.opsForValue().set("allStudents", studentList);
                } else {
                    System.out.println("查询的缓存.......");
                }
            }
        } else {
            System.out.println("查询的缓存.......");
        }
        return studentList;
    }*/

    @Transactional
    @Override
    public int update() {
        return up();
    }

    public int up() {
        Student student = new Student();
        student.setId(1);
        student.setName("李四-update");
        student.setAge(18);
        int update = studentMapper.updateByPrimaryKeySelective(student);
        System.out.println("更新结果：" + update);
        //除数不能为0，所以此处会抛出一个运行时的异常，上一步的更新就会回滚
        int a = 10 / 0;
        return update;
    }

    @Override
    public Object queryTest() {
        List<Map<String, Object>> list = studentMapper.queryTest();
        Map<String, List<Map<String, Object>>> map = new HashMap<>();

        Set<String> years = new HashSet<>();
        for (Map<String, Object> m : list) {
            years.add(m.get("nd").toString());
            String type = m.get("type").toString();
            if (map.get(type) != null) {
                map.get(type).add(m);
            } else {
                List<Map<String, Object>> l = new ArrayList<>();
                l.add(m);
                map.put(type, l);
            }
        }
        Set<Map.Entry<String, List<Map<String, Object>>>> entries = map.entrySet();
        for (String year : years) {
            for (Map.Entry<String, List<Map<String, Object>>> e : entries) {
                boolean isExist = false;
                List<Map<String, Object>> values = e.getValue();
                for (Map<String, Object> v : values) {
                    if (year.equals(v.get("nd"))) {
                        isExist = true;
                        break;
                    }
                }
                if (!isExist) {
                    Map<String, Object> item = new HashMap<>();
                    item.put("nd", year);
                    item.put("value", 0);
                    values.add(item);
                }
            }
        }
        for (Map.Entry<String, List<Map<String, Object>>> e : entries) {
            List<Map<String, Object>> values = e.getValue();
            values.sort(new Comparator<Map<String, Object>>() {
                @Override
                public int compare(Map<String, Object> o1, Map<String, Object> o2) {
                    String nd1 = o1.get("nd").toString();
                    String nd2 = o2.get("nd").toString();
                    return nd1.compareTo(nd2);
                }
            });
        }
        System.out.println(map);
        return map;
    }

    @Override
    public List<Staff> queryUser() {
        return studentMapper.queryUser();
    }
}